import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:demo/providers/counter_providers.dart';

void main() {
  group('Riverpod Widget Tests', () {
    Widget createTestWidget() {
      return ProviderScope(
        child: MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: const Text('Test')),
            body: Column(
              children: [
                // 显示计数器值
                Consumer(
                  builder: (context, ref, child) {
                    final count = ref.watch(counterValueProvider);
                    return Text('Counter: $count');
                  },
                ),
                
                // 显示用户名
                Consumer(
                  builder: (context, ref, child) {
                    final username = ref.watch(usernameProvider);
                    return Text('User: $username');
                  },
                ),
                
                // 显示状态
                Consumer(
                  builder: (context, ref, child) {
                    final status = ref.watch(statusProvider);
                    return Text('Status: $status');
                  },
                ),
                
                // 显示是否为偶数
                Consumer(
                  builder: (context, ref, child) {
                    final isEven = ref.watch(isEvenProvider);
                    return Text('Is Even: ${isEven ? 'Yes' : 'No'}');
                  },
                ),
                
                // 增加按钮
                Consumer(
                  builder: (context, ref, child) {
                    return ElevatedButton(
                      onPressed: () => ref.read(counterProvider.notifier).increment(),
                      child: const Text('Increment'),
                    );
                  },
                ),
                
                // 重置按钮
                Consumer(
                  builder: (context, ref, child) {
                    return ElevatedButton(
                      onPressed: () => ref.read(counterProvider.notifier).reset(),
                      child: const Text('Reset'),
                    );
                  },
                ),
                
                // 设置用户名按钮
                Consumer(
                  builder: (context, ref, child) {
                    return ElevatedButton(
                      onPressed: () => ref.read(counterProvider.notifier).setUsername('Alice'),
                      child: const Text('Set User'),
                    );
                  },
                ),
                
                // 批量增加按钮
                Consumer(
                  builder: (context, ref, child) {
                    return ElevatedButton(
                      onPressed: () => ref.read(counterProvider.notifier).addMultiple(10),
                      child: const Text('Add 10'),
                    );
                  },
                ),
              ],
            ),
          ),
        ),
      );
    }

    testWidgets('应该显示初始状态', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证初始状态
      expect(find.text('Counter: 0'), findsOneWidget);
      expect(find.text('User: Guest'), findsOneWidget);
      expect(find.text('Status: Zero'), findsOneWidget);
      expect(find.text('Is Even: Yes'), findsOneWidget);
    });

    testWidgets('点击Increment按钮应该增加计数器', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 查找并点击Increment按钮
      final incrementButton = find.text('Increment');
      expect(incrementButton, findsOneWidget);

      await tester.tap(incrementButton);
      await tester.pump();

      // 验证计数器增加了
      expect(find.text('Counter: 1'), findsOneWidget);
      expect(find.text('Status: Low'), findsOneWidget);
      expect(find.text('Is Even: No'), findsOneWidget);
    });

    testWidgets('点击Reset按钮应该重置计数器', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 先增加计数器
      final incrementButton = find.text('Increment');
      await tester.tap(incrementButton);
      await tester.tap(incrementButton);
      await tester.pump();

      // 验证计数器增加了
      expect(find.text('Counter: 2'), findsOneWidget);

      // 查找并点击Reset按钮
      final resetButton = find.text('Reset');
      expect(resetButton, findsOneWidget);

      await tester.tap(resetButton);
      await tester.pump();

      // 验证计数器重置了
      expect(find.text('Counter: 0'), findsOneWidget);
      expect(find.text('Status: Zero'), findsOneWidget);
      expect(find.text('Is Even: Yes'), findsOneWidget);
    });

    testWidgets('点击Set User按钮应该更新用户名', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证初始用户名
      expect(find.text('User: Guest'), findsOneWidget);

      // 查找并点击Set User按钮
      final setUserButton = find.text('Set User');
      expect(setUserButton, findsOneWidget);

      await tester.tap(setUserButton);
      await tester.pump();

      // 验证用户名更新了
      expect(find.text('User: Alice'), findsOneWidget);
    });

    testWidgets('点击Add 10按钮应该批量增加计数器', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 查找并点击Add 10按钮
      final add10Button = find.text('Add 10');
      expect(add10Button, findsOneWidget);

      await tester.tap(add10Button);
      await tester.pump();

      // 验证计数器增加了10
      expect(find.text('Counter: 10'), findsOneWidget);
      expect(find.text('Status: Medium'), findsOneWidget);
      expect(find.text('Is Even: Yes'), findsOneWidget);
    });

    testWidgets('多个Consumer应该同步更新', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证初始状态
      expect(find.text('Counter: 0'), findsOneWidget);
      expect(find.text('Status: Zero'), findsOneWidget);
      expect(find.text('Is Even: Yes'), findsOneWidget);

      // 点击Increment按钮
      final incrementButton = find.text('Increment');
      await tester.tap(incrementButton);
      await tester.pump();

      // 验证所有相关的Widget都更新了
      expect(find.text('Counter: 1'), findsOneWidget);
      expect(find.text('Status: Low'), findsOneWidget);
      expect(find.text('Is Even: No'), findsOneWidget);

      // 再次点击
      await tester.tap(incrementButton);
      await tester.pump();

      expect(find.text('Counter: 2'), findsOneWidget);
      expect(find.text('Status: Low'), findsOneWidget);
      expect(find.text('Is Even: Yes'), findsOneWidget);
    });

    testWidgets('状态应该根据计数器值正确更新', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 测试不同的状态
      expect(find.text('Status: Zero'), findsOneWidget);

      // 增加到Low状态
      final incrementButton = find.text('Increment');
      for (int i = 0; i < 5; i++) {
        await tester.tap(incrementButton);
        await tester.pump();
      }
      expect(find.text('Status: Low'), findsOneWidget);

      // 增加到Medium状态
      final add10Button = find.text('Add 10');
      await tester.tap(add10Button);
      await tester.pump();
      expect(find.text('Status: Medium'), findsOneWidget);
    });

    testWidgets('Provider状态应该在Widget重建时保持', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 修改状态
      final incrementButton = find.text('Increment');
      await tester.tap(incrementButton);
      await tester.pump();

      expect(find.text('Counter: 1'), findsOneWidget);

      // 重建Widget
      await tester.pumpWidget(createTestWidget());

      // 状态应该保持（因为ProviderScope没有改变）
      expect(find.text('Counter: 1'), findsOneWidget);
    });

    testWidgets('多个操作的组合测试', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 设置用户名
      await tester.tap(find.text('Set User'));
      await tester.pump();
      expect(find.text('User: Alice'), findsOneWidget);

      // 增加计数器
      await tester.tap(find.text('Add 10'));
      await tester.pump();
      expect(find.text('Counter: 10'), findsOneWidget);
      expect(find.text('Status: Medium'), findsOneWidget);

      // 继续增加
      await tester.tap(find.text('Increment'));
      await tester.tap(find.text('Increment'));
      await tester.pump();
      expect(find.text('Counter: 12'), findsOneWidget);
      expect(find.text('Is Even: Yes'), findsOneWidget);

      // 重置
      await tester.tap(find.text('Reset'));
      await tester.pump();
      expect(find.text('Counter: 0'), findsOneWidget);
      expect(find.text('Status: Zero'), findsOneWidget);
      expect(find.text('User: Alice'), findsOneWidget); // 用户名应该保持不变
    });
  });
}
